<?php
namespace app\common\model;

use think\Db;

class News extends Common
{
    private $table_name = '';
    private $news_table = 'hn_news';
    /** 封装一个新闻对象 */
    public function fillNews($id, $title, $subtitle, $kind, $desc, $createon, $author, $source, $sort)
    {
        $model = ['id' => $id, 'title' => $title, 'subTitle' => $subtitle, 'kind' => $kind, 'description' => $desc, 'createOn' => $createon, 'author' => $author, 'source' => $source, 'sort' => $sort];
        return $model;
    }
    /** 获取一个新闻信息 */
    public function getNewsInfo($id)
    {
        $model = Db::table($this->table_name)->find();
        return $model;
    }
    /** 获取新闻列表 */
    public function getNewsList($kind, $keyword, $take, $pages)
    {
        $query = Db::table($this->table_name);
        if (strlen($kind) > 0) {
            $query = $query->where('kind', $kind);
        }
        if (strlen($keyword) > 0) {
            $query = $query->where('title', 'like', $keyword);
        }

        $sort="";
        $lst = $query->limit($take)->page($sort)->select();
        return $lst;
    }
    /** 创建一则新闻 */
    public function createNews($model)
    {
        return $model->id;
    }
    /** 修改新闻 */
    public function updateNews($id, $model)
    {
        return ['flag' => \FALSE, 'msg' => ''];
    }
    /** 删除一则新闻 */
    public function removeNews($id)
    {
        return ['flag' => \FALSE, 'msg' => ''];
    }

    /**
     * 保存或更新新闻数据
     * @param $data
     * @return bool
     */
    public function saveNews($data)
    {

        $need=["id","title","subtitle","desc","type","kind","createon","author","source","sort","target","create_time"];

        $r=$this->getNeed($need,$data);

        $res=$this->saveData($r,$this->news_table);

        if($res){
            return $res !== false;
        }else{
            return $res !== false;
        }

    }

    /**
     * 删除新闻
     * @param $id
     * @return bool
     */
    public function delNews($id)
    {
        $res=Db::table($this->news_table)
            ->where('id', $id)
            ->update(['is_deleted' => 1]);

        return $res !== false;
    }
}